<?php

namespace app\marketing\controller;

use app\ApiRest;
use \app\marketing\model\Poster as PosterModel;
use app\marketing\model\MyPoster as MyPosterModel;

class IndexPoster extends ApiRest
{
    public function listAllPoster()
    {
        //筛选部门信息
        $param = $this->_param;
        //获取分页信息
        $page_config = array(
            'page' => 1,
            'page_count' => 20
        );
        //设置页码
        if (isset($param['page']) && $param['page'] > 0) $page_config['page'] = $param['page'];
        //设置每页的数据
        if (isset($param['page_count']) && $param['page_count'] > 0) $page_config['page_count'] = $param['page_count'];
        $filter[] = ['uniacid', '=', $this->_uniacid];

        $filter[] = ['status', '=', 1];

        if (isset($param['type_id']) && $param['type_id'] > 0) {
            $filter[] = ['type_id', '=', $param['type_id']];
        }
        $m_poster = new PosterModel();
        $page_config['total'] = $m_poster->listPostersCount($filter);
        $posters = $m_poster->listPosters($filter, $page_config);
        $page_config['total_page'] = (int)($page_config['total'] / $page_config['page_count']);
        if (($page_config['total'] % $page_config['page_count']) > 0) $page_config['total_page'] = $page_config['total_page'] + 1;
        //设置返回参数
        $result = $page_config;
        //返回数据
        $posters = transImages($posters,['cover', 'img'], ';');
        $result['data'] = $posters;
        return $this->success($result);


    }

    public function listMyPoster()
    {
        //筛选部门信息
        $param = $this->_param;
        //获取分页信息
        $page_config = array(
            'page' => 1,
            'page_count' => 20
        );

        //设置页码
        if (isset($param['page']) && $param['page'] > 0) $page_config['page'] = $param['page'];
        //设置每页的数据
        if (isset($param['page_count']) && $param['page_count'] > 0) $page_config['page_count'] = $param['page_count'];


        $user_id = $this->_user_id;

        //用于接口测试的参数 --- onlyForTestUserId
        if ($this->_user_id === null && isset($param['onlyForTestUserId']))
        {
            $user_id = $param['onlyForTestUserId'];
        }
        $filter[] = ['uniacid', '=', $this->_uniacid];
        $filter[] = ['user_id', '=', $user_id];
        $filter[] = ['status', '=', 1];
        if (isset($param['type_id']) && $param['type_id'] > 0)
        {
            $filter[] = ['type_id', '=', $param['type_id']];
        }

        $m_poster = new MyPosterModel();
        $page_config['total'] = $m_poster->listCount($filter);
        $posters = $m_poster->list($filter, $page_config);
        $page_config['total_page'] = (int)($page_config['total'] / $page_config['page_count']);
        if (($page_config['total'] % $page_config['page_count']) > 0) $page_config['total_page'] = $page_config['total_page'] + 1;
        //设置返回参数
        $result = $page_config;
        //返回数据
        $posters = transImages($posters,['cover', 'img'], ';');
        $result['data'] = $posters;

        return $this->success($result);


    }

    public function createMyPoster()
    {
        $input = $this->_input;
        $user_id = $this->_user_id ?? $input['onlyForTestUserId'] ?? null;

        if (!$user_id)
        {
            return $this->error('user_id is required');
        }

        $title = $input['title'] ?? null;
        if (!$title)
        {
            return $this->error('title is required');
        }

        $img = $input['img'] ?? null;
        if (!$img)
        {
            return $this->error('img is required');
        }


        $onePosterData = [
            'title' => $title,
            'img' => $img,
            'user_id' => $user_id,
            'top' => 0,
            'uniacid' => $this->_uniacid
        ];
        $poster = new MyPosterModel();
        $res = $poster->createOne($onePosterData);
        return $this->success($res);
    }

    public function delMyPoster()
    {
        if (empty($this->_input['poster_id'])) {
            return $this->error('poster_id is required');
        }
        $poster_id   = $this->_input['poster_id'];
        $model = new MyPosterModel();
        $rst = $model->updateOne(['id' => $poster_id], ['status' => -1]);
        return $this->success($rst);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-26 14:51
     * @功能说明:获取海报码样式
     */
    public function getMarketingType(){

        $key = $this->getUserId().'market_type';

        $data= getCache($key,$this->_uniacid);

        $data= !empty($data)?$data:0;

        return $this->success($data);
    }


    /**
     * @author chenniang
     * @DataTime: 2021-05-26 14:56
     * @功能说明:设置海报码样式
     */
    public function setMarketingType(){

        $input = $this->_input;

        $key = $this->getUserId().'market_type';

        setCache($key,$input['type'],0,$this->_uniacid);

        return $this->success(true);

    }

}
